/*
 * -----------------------------------------------------------------------
 * Copyright 2012 - Alistair Rutherford - www.netthreads.co.uk
 * -----------------------------------------------------------------------
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package com.garrigou.spaceship.layer;

import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.garrigou.spaceship.action.ActionCallBack;
import com.garrigou.spaceship.actor.Ship2D;
import com.garrigou.spaceship.constants.ActorEvents;
import com.garrigou.spaceship.director.Director;
import com.garrigou.spaceship.domain.Profile;
import com.garrigou.spaceship.event.ActorEvent;
import com.garrigou.spaceship.event.ActorEventObserver;
import com.garrigou.spaceship.scenes.Layer;

/**
 * Scene layer.
 * 
 */
public class ShipLayer extends Layer implements ActorEventObserver, ActionCallBack
{
    private static final float CONTROL_INTERVAL       = 0.75f; // Pulse rate.

    public static final float  TEXTURE_FRAME_DURATION = 0.5f;

    private float              offsetX;
    private float              offsetY;

    private Profile            profile;
    private TextureAtlas       atlas;

    // Director of the action.
    private Director           director;
    private Ship2D ship;
    
    public Ship2D getShip()
    {
        return ship;
    }

    /**
     * Create layer.
     * 
     * @param stage
     */
    public ShipLayer(float width, float height, Profile prof, TextureAtlas textureAtlas)
    {
        setSize(width, height);

        this.profile = prof;
        this.director = Director.instance();
        this.atlas = textureAtlas;
// gameProperties = GameProperties.instance();

        buildElements();

// buildActions();
    }

    /**
     * Called when layer is part of visible view but not yet displayed.
     * 
     */
    @Override
    public void enter()
    {
        // Add this as an event observer.
        director.registerEventHandler(this);
    }

    /**
     * Called when layer is no longer part of visible view.
     * 
     */
    @Override
    public void exit()
    {
        // Remove this as an event observer.
        director.deregisterEventHandler(this);
    }

    /**
     * Build view elements.
     * 
     */
    private void buildElements()
    {
// TextureCache textureCache = TextureCache.instance();
//
// TextureDefinition definition = textureCache.getDefinition(AppActorTextures.TEXTURE_SHIPS);
// TextureRegion textureRegion = textureCache.getTexture(definition);

        // ---------------------------------------------------------------
        // Space ship
        // ---------------------------------------------------------------
        ship = Ship2D.create(profile.getShip(), atlas);
        ship.setInitialPosition((getWidth() / 2 - ship.getWidth() / 2), ship.getHeight());
        
        addActor(ship);
        
    }

    /**
     * Create timer action to start layer elements.
     * 
     */
//  private void buildActions()
//  {
//    CallBackDelayAction callBackDelay = CallBackDelayAction.$(-1, CONTROL_INTERVAL, this);
//
//    action(callBackDelay);
//  }

    /**
     * Every delay duration make a decision about what to do next.
     * 
     */
    @Override
    public void onCallBack()
    {
        // Launch an asteroid.
        this.director.sendEvent(ActorEvents.EVENT_START_PULSE, ship);

        // Play zap.
//        if (gameProperties.isAudioOn())
//        {
//            SoundCache.instance().get(AppActorSounds.SOUND_PULSE).play(gameProperties.getVolume());
//        }
    }

//    @Override
//    public boolean touchDown(int x, int y, int pointer, int button)
//    {
//        float touchX = (x * director.getScaleFactorX()) - offsetX;
//        float touchY = director.getHeight() - (y * director.getScaleFactorY()) + offsetY;
//
//        spriteShip.trackToXY(touchX, touchY);
//
//        return false;
//    }
//
//    @Override
//    public boolean touchDragged(int x, int y, int pointer)
//    {
//        float touchX = (x * director.getScaleFactorX()) - offsetX;
//        float touchY = director.getHeight() - (y * director.getScaleFactorY()) + offsetY;
//
//        spriteShip.trackToXY(touchX, touchY);
//
//        return true;
//    }

    /**
     * Handle events.
     * 
     */
    @Override
    public boolean handleEvent(ActorEvent event)
    {
        boolean handled = false;

        switch (event.getId())
        {
            case ActorEvents.EVENT_COLLISION_ASTEROID_SHIP:
                handleCollision(event.getActor());
                handled = true;
            break;
            default:
            break;
        }

        return handled;
    }

    /**
     * Handle hitting asteroid.
     * 
     * @param actor
     */
    private void handleCollision(Actor actor)
    {
        ship.runCollision();
    }
}
